//
//  PostsItemPicView.m
//  JiaoX
//
//  Created by yinqy on 15/3/3.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "PostsItemPicView.h"
@interface PostsItemPicView()

@property (nonatomic,strong)NSMutableArray *imageArray;
@property (nonatomic,weak) UIButton *numBtn;

@end
@implementation PostsItemPicView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        CGFloat WIDTH = (JXScreenW-50)/4;
        [self setSize:CGSizeMake(JXScreenW, WIDTH)];
        self.imageArray = [NSMutableArray array];
        
        //添加imageview
        for (int i = 0; i<4; i++) {
            UIImageView *imageView = [[UIImageView alloc]init];
            [imageView setSize:CGSizeMake(WIDTH, WIDTH)];
            [imageView setX:i*(WIDTH+10)+10];
            [imageView setTag:i+1000];
            [self addSubview:imageView];
            [self.imageArray addObject:imageView];
        }
        //添加
        UIButton *numBtn = [[UIButton alloc]init];
        [numBtn setBackgroundImage:[UIImage resizedImage:@"bg_pic_num"] forState:UIControlStateNormal];
        [numBtn setUserInteractionEnabled:NO];
        [numBtn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:10]]];
        [numBtn setHidden:YES];
        [self addSubview:numBtn];
        self.numBtn = numBtn;
    }
    return self;
}

-(void)setImages:(NSArray *)images{
    if (images.count>4) {
        [self.numBtn setHidden:NO];
        [self.numBtn setTitle:[NSString stringWithFormat:@"共%lu张",(unsigned long)images.count] forState:UIControlStateNormal];
        CGSize size = [self.numBtn.titleLabel.text sizeWithFont:self.numBtn.titleLabel.font];
        [self.numBtn setSize:CGSizeMake(size.width+10, size.height+4)];
        [self.numBtn setX:self.width-10-self.numBtn.width];
        [self.numBtn setY:self.height-5-self.numBtn.height];
    }else{
        [self.numBtn setHidden:YES];
    }
    for (int i = 0; i<4; i++) {
        if (i+1>images.count) {
            [[self viewWithTag:i+1000] setHidden:YES];
        }else{
            UIImageView *image = (UIImageView *)[self viewWithTag:i+1000];
            [image setHidden:NO];
            [image sd_setImageWithURL:[NSURL URLWithString:images[i]]];
        }
    }
}
@end
